<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

use app\library\Flag;

class MsgRecords extends Model
{
    protected $table = 'msg_records';

    protected $pk = 'id';

    /**
     * 添加记录
     *
     */
    public function add($user_id, $recv_user_id)
    {
        $sql = sprintf("insert into %s(user_id, recv_user_id, create_time) values(%d,%d,%d) on duplicate key update update_time=%d", $this->table, $user_id, $recv_user_id, time(), time());
        return Db::table($this->table)->query($sql);
    }

    /**
     * 删除记录
     *
     */
    public function del($userid, $recv_user_id)
    {

        $conds = [
            'user_id' => ['=', $userid],
            'recv_user_id' => ['=', $recv_user_id],
        ];

        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * 获取消息记录
     *
     */
    public function getMsgRecords($conds = '1=1', $offset, $limit)
    {
        $ret = Db::table($this->table)->where($conds)->order('update_time desc')->limit($offset, $limit)->select();

        if (empty($ret)) {
            return [];
        } else {
            return $ret;
        }
    }

}
